<?php
/**
 * @author Erling Owe <erling.owe@gmail.com>
 * @package Core
 * @subpackage Presentation
 */

class Core_Presentation_HTML_Document extends Core_Presentation_HTML_Element {

    private $constDocumentType = Core_Presentation_DocumentTypes::HTML5;

    private $NodeCollection;

    public $Title;
    public $Base;
    
    private $arrCSSs = array();
    private $arrJSs = array();

    private $strTab = "    ";

    function __construct ( $strHTML = null ) {
        if ( $strHTML ) {
            // TODO: Create the document from HTML.
        } else {
            // Create a new document.
            parent::__construct( $this, "body" );
        }
        $this->NodeCollection = new Core_Presentation_HTML_NodeCollection ();
    }

    /**
     * Adds a stylesheet to the document.
     *
     * @param string $strURI URI (relative to the base, if set).
     */
    public function addCSS ( $strURI ) {
        $this->arrCSSs[] = $strURI;
    }

    public function addJS ( $strURI ) {
        $this->arrJSs[] = $strURI;
    }

    public function addScript ( $strScript ) {

    }

    /**
     * Creates an element.
     *
     * @param string $strTagName
     * @return Core_Presentation_HTML_Element
     */
    public function createElement ( $strTagName ) {
        $objElement = new Core_Presentation_HTML_Element ( $this, $strTagName );
        $this->NodeCollection[] = $objElement;
        return $objElement;
    }

    /**
     * Creates a text node.
     *
     * @param string $strText
     * @return Core_Presentation_HTML_TextNode
     */
    public function createTextNode ( $strText ) {
        $objTextNode = new Core_Presentation_HTML_TextNode ( $this, $strText );
        $this->NodeCollection[] = $objTextNode;
        return $objTextNode;

    }

    public function createWidget () {

    }

    /**
     * Gets the char(s) used as tab.
     *
     * @return string
     */
    public function getTab () {
        return $this->strTab;
    }

    /**
     * Sets the char(s) used as tab.
     *
     * This can only be space or tab chars.
     *
     * @param string $strTab
     */
    public function setTab ( $strTab ) {
        $this->strTab = $strTab;
    }

    /**
     * Gets the HTML for the entire document.
     *
     * @return string
     */
    public function getHTML () {

        switch ( $this->constDocumentType ) {

            case Core_Presentation_DocumentTypes::HTML5:

                $strReturn = "<!DOCTYPE html>\r\n\r\n";
                $strReturn .= "<html>\r\n\r\n";
                $strReturn .= "<head>\r\n";
                $strReturn .= $this->getTab() . "<title>" . $this->Title . "</title>\r\n";

                if ( $this->Base ) {
                    $strReturn .= $this->getTab() . '<base href="' . $this->Base . '">' . "\r\n";
                }

                foreach ( $this->arrCSSs as $strURI ) {
                    $strReturn .= $this->getTab() . '<link rel="stylesheet" type="text/css" href="' . $strURI . '">' . "\r\n";
                }

                $strReturn .= "</head>\r\n\r\n";

                $strReturn .= parent::getHTML();

                $strReturn .= "\r\n</html>\r\n";

                break;

        }

        return $strReturn;

    }

    public function setDocumentType ( $constDocumentType ) {

        $this->constDocumentType = $constDocumentType;

    }

    /**
     * Gets an instance of Core_Presentation_HTML_NodeCollection.
     *
     * Gets an instance of Core_Presentation_HTML_NodeCollection containing
     * all the elements within the <body></body> tag matching the expression.
     *
     * @param string $strExpression
     * @return Core_Presentation_HTML_NodeCollection
     */
    public function find ( $strExpression = null ) {
        return $this->NodeCollection->find( $strExpression );
    }

}

?>